package com.yunhe.spring.dao.impl;


import com.yunhe.spring.dao.StudentDao;
import com.yunhe.spring.entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDaoImpl implements StudentDao {
    @Autowired
    private SessionFactory sessionFactory;
    @Override
    public void save(Student student) {
        Session session = sessionFactory.getCurrentSession();
         session.save(student);

    }

    @Override
    public void remove(Integer id) {
        Session session = sessionFactory.getCurrentSession();
        Student student = session.find(Student.class, id);
        session.remove(student);
    }

    @Override
    public Student findById(Integer id) {
        Session session = sessionFactory.getCurrentSession();
        Student student = session.find(Student.class, id);
         return student;
    }

    @Override
    public List<Student> findAll() {
        Session session = sessionFactory.getCurrentSession();
        List<Student> list = session.createQuery("from Student").list();
        return list;
    }
}
